package com.q.tqb.activity.mine;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.MainActivity;
import com.q.tqb.activity.mine.wallet.WalletWithdrawActivity;
import com.q.tqb.adapter.CardAdapter;
import com.q.tqb.adapter.LevelAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.LevelInfo;
import com.q.tqb.model.UserInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.IResponse2;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.dialog.PayPassDialog;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangeLevelActivity extends BaseActivity {

    @BindView(R.id.tv_tip)
    TextView tv_tip;
    @BindView(R.id.rv_card)
    GRecyclerView mRvCard;
    @BindView(R.id.tv_check)
    TextView tv_check;
    @BindView(R.id.et_ye)
    EditText et_ye;
    @BindView(R.id.et_community)
    EditText et_community;


    private ArrayList<LevelInfo> mMineCardInfos = new ArrayList<>();
    private LevelAdapter levelAdapter;
    private LevelInfo levelInfo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_level;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("会员升级");

        mRvCard.setLoadingMoreEnabled(false);
        mRvCard.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getLevel();
            }

            @Override
            public void onLoadMore() {
            }
        });
        levelAdapter = new LevelAdapter(ChangeLevelActivity.this, mMineCardInfos);
        levelAdapter.setOnItemClickListener(new LevelAdapter.OnItemClickListener() {
            @Override
            public void onClick(final int position) {
                levelAdapter.check(position);
                levelInfo = mMineCardInfos.get(position);
                tv_check.setText("已选择:" + "LV" + levelInfo.level + ":" + levelInfo.level_name);
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ChangeLevelActivity.this);
        mRvCard.setLayoutManager(linearLayoutManager);
        mRvCard.setAdapter(levelAdapter);
        mRvCard.setRefresh();

    }

    @OnClick({R.id.iv_back, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_ok:
                doLevel();
                break;
            default:
                break;
        }
    }

    private void getLevel() {
        int uid = (int) SPUtil.get(this, "uid", 0);
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        OkHttpUtil.getInstance().post2(ChangeLevelActivity.this, AppConst.URL_GET_UPGRADE, map, new IResponse2() {

            @Override
            public void onSuccess(JsonElement element, String msg) {
                Gson gson = new Gson();
                ArrayList<LevelInfo> list = gson.fromJson(element, new TypeToken<ArrayList<LevelInfo>>() {
                }.getType());
                mRvCard.refreshComplete();
                mMineCardInfos.clear();
                mMineCardInfos.addAll(list);
                levelAdapter.notifyDataSetChanged();

                tv_tip.setText(msg);
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                mRvCard.refreshComplete();
                showToast(errorMsg);
            }
        });
    }
    private void doLevel(){
        if (levelInfo==null){
            showToast("请选择要升级的会员");
            return;
        }
        if (TextUtils.isEmpty(et_ye.getText().toString())){
            showToast("请输入余额");
            return;
        }
        double douInputMoney=Double.parseDouble(et_ye.getText().toString());
        PayPassDialog payPassDialog = new PayPassDialog(ChangeLevelActivity.this, douInputMoney, 1);
        payPassDialog.show();
        showInputMethod();
        payPassDialog.setOnSettingListener(new PayPassDialog.CommonListener() {
            @Override
            public void onClick(String pass) {
                mProgressDialog.show();
                int uid = (int) SPUtil.get(ChangeLevelActivity.this, "uid", 0);
                Map<String, String> map = new HashMap<>();
                map.put("uid", uid + "");
                map.put("token", StringUtil.getToken(uid));

                map.put("level", levelInfo.level);
                map.put("balance", et_ye.getText().toString());
                map.put("cash", et_community.getText().toString());
                map.put("pay_password", pass);

                OkHttpUtil.getInstance().post2(ChangeLevelActivity.this, AppConst.URL_UPGRADE, map, new IResponse2() {

                    @Override
                    public void onSuccess(JsonElement element, String msg) {
                        Gson gson = new Gson();
                        String result = element.toString().replace("null", "\"\"");
                        UserInfo personData = gson.fromJson(result, UserInfo.class);

                        SPUtil.put(ChangeLevelActivity.this, "level", personData.getLevel());
                        SPUtil.put(ChangeLevelActivity.this, "level_name", personData.getLevel_name());
                        SPUtil.put(ChangeLevelActivity.this, "is_max_level", personData.getIs_max_level());
                        mProgressDialog.dismiss();
                        showToast(msg);
                        ChangeLevelActivity.this.finish();
                    }

                    @Override
                    public void onFailure(int statusCode, String errorMsg) {
                        mProgressDialog.dismiss();
                        showToast(errorMsg);
                    }
                });
            }

        });

    }
    private void showInputMethod() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 100);
    }

}
